package xin.marcher.module.replenished.remote;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
import xin.marcher.framework.common.util.data.DataCuttingUtil;
import xin.marcher.module.common.constants.replenished.RecoverableConstants;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.lifecycle.api.ItemPeriodStageApi;
import xin.marcher.module.lifecycle.domain.dto.ItemExpriResultDTO;
import xin.marcher.module.lifecycle.domain.request.QueryPeriodListRequest;
import xin.marcher.module.replenished.exception.ReplenishedBizException;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 生命周期 API
 *
 *
 */
@Component
public class ItemPeriodStageRemote {

    @DubboReference(version = "1.0.0")
    private ItemPeriodStageApi itemPeriodStageApi;

    /**
     * 根据商品id批量查询商品考核结果（包括可采可补信息）
     *
     * @param itemIdList
     * @return
     */
    public List<ItemExpriResultDTO> queryByItemIds(Set<String> itemIdList) {
        List<ItemExpriResultDTO> itemExpriResultDTOList = new ArrayList<>(itemIdList.size());
        // 批量查询，对itemId进行分割，批量查询后返回
        List<Set<String>> itemIdSplitList = DataCuttingUtil.dataCuttingString(itemIdList, RecoverableConstants.ITEM_LIMIT_NUM);
        for (Set<String> itemSet : itemIdSplitList) {
            QueryPeriodListRequest request = new QueryPeriodListRequest();
            List<String> itemList = new ArrayList<>(itemSet);
            request.setItemIdList(itemList);
            JsonResult<List<ItemExpriResultDTO>> jsonResult = itemPeriodStageApi.queryByItemIds(request);
            if (!jsonResult.getSuccess()) {
                throw new ReplenishedBizException(jsonResult.getErrorCode(), jsonResult.getErrorMessage());
            }
            itemExpriResultDTOList.addAll(jsonResult.getData());
        }
        return itemExpriResultDTOList;
    }
}
